home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / bstate.zip / BSTATEB.CPP next >
C/C++ Source or Header  |  1993-09-16  |  3KB  |  160 lines

  1. // bstateb.cpp : implementation file
  2. //
  3.  
  4. #include <afxwin.h>
  5. #include <afxdlgs.h>
  6. #include <afxext.h>
  7. #include "bstateb.h"
  8.  
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char BASED_CODE THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CBStateButton
  16.  
  17. IMPLEMENT_DYNAMIC(CBStateButton, CButton)
  18.  
  19. CBStateButton::CBStateButton(BOOL bCenter, UINT nID, CWnd *pParent)
  20. {
  21.     subclassed = FALSE;
  22.     if (pParent != NULL) {
  23.         if (SubclassDlgItem(nID, pParent)) {
  24.             subclassed = TRUE;
  25.         }
  26.     }
  27.     
  28.     numStates = 0;
  29.     center = bCenter;
  30.     curStateInd = -1;
  31. }
  32.  
  33. CBStateButton::~CBStateButton()
  34. {
  35.     UINT i;
  36.     
  37.     if (numStates > 0) {
  38.         i = 0;
  39.         while (i < numStates) {
  40.             delete pictures[i];
  41.             i++;
  42.         }
  43.     }
  44. }
  45.  
  46. void CBStateButton::Center(BOOL bCenter)
  47. {
  48.     center = bCenter;
  49.     Invalidate(TRUE);
  50. }
  51.  
  52. void CBStateButton::AttachToButton(UINT nID, CWnd *pParent)
  53. {
  54.     if (SubclassDlgItem(nID, pParent)) {
  55.         subclassed = TRUE;
  56.     }
  57. }
  58.  
  59. void CBStateButton::AddState(UINT stval, UINT bmid)
  60. {
  61.     DoAddState(stval, bmid);
  62. }
  63.  
  64. void CBStateButton::AddState(UINT stval)
  65. {
  66.     DoAddState(stval, stval);
  67. }
  68.  
  69. void CBStateButton::DoAddState(UINT stval, UINT bmid)
  70. {
  71.     CBitmap *newbm;
  72.     UINT i;
  73.  
  74.     newbm = new CBitmap();
  75.     if (!newbm->LoadBitmap(bmid)) {
  76.         return;
  77.     }
  78.     
  79.     if (numStates == 0) {
  80.         numStates = 1;
  81.         states[0] = stval;
  82.         pictures[0] = newbm;
  83.         return;
  84.     }
  85.     
  86.     i = 0;
  87.     while (i < numStates) {
  88.         if (states[i] == stval) {
  89.             delete pictures[i];
  90.             pictures[i] = newbm;
  91.             return;
  92.         }
  93.         i++;
  94.     }
  95.         
  96.     if (numStates >= _CBSTATEMAX_ - 1) {
  97.         delete newbm;
  98.         return;
  99.     }
  100.     
  101.     states[numStates] = stval;
  102.     pictures[numStates] = newbm; 
  103.     
  104.     numStates++;
  105. }
  106.  
  107. void CBStateButton::SetStateNumber(UINT stval)
  108. {
  109.     UINT i;
  110.     
  111.     if (numStates == 0) {
  112.         return;
  113.     }
  114.     
  115.     i = 0;
  116.     while (i < numStates) {
  117.         if (states[i] == stval) {
  118.             curStateInd = i;
  119.             Invalidate(TRUE);
  120.             return;
  121.         }
  122.         i++;
  123.     }
  124. }
  125.  
  126. int CBStateButton::GetStateNumber(void)
  127. {
  128.     if (curStateInd == -1) {
  129.         return -1;
  130.     }
  131.     return states[curStateInd];
  132. }
  133.  
  134. void CBStateButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  135. {
  136.     if (curStateInd == -1 || numStates == 0) {
  137.         return;
  138.     }
  139.     
  140.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  141.     CDC memDC;
  142.     memDC.CreateCompatibleDC(pDC);
  143.     CBitmap* pOld = memDC.SelectObject(pictures[curStateInd]);
  144.     if (pOld == NULL)
  145.         return;     // destructors will clean up
  146.  
  147.     CRect rect;
  148.     rect.CopyRect(&lpDIS->rcItem);
  149.     
  150.     // TODO: If bitmap is smaller than the area, fill in with the
  151.     // background and center the bitmap if requested
  152.     
  153.     pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  154.         &memDC, 0, 0, SRCCOPY);
  155.     memDC.SelectObject(pOld);
  156. }
  157.  
  158.  
  159.  
  160.